home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / mm / mm-0.90 / buildhelp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-12-18  |  4.6 KB  |  196 lines

  1. /*
  2.  * Copyright (c) 1986, 1990 by The Trustees of Columbia University in
  3.  * the City of New York.  Permission is granted to any individual or
  4.  * institution to use, copy, or redistribute this software so long as it
  5.  * is not sold for profit, provided this copyright notice is retained.
  6.  */
  7.  
  8. #ifndef lint
  9. static char *rcsid = "$Header: /f/src2/encore.bin/cucca/mm/tarring-it-up/RCS/buildhelp.c,v 2.1 90/10/04 18:23:36 melissa Exp $";
  10. #endif
  11.  
  12. /*
  13.  * buildhelp:
  14.  * read in help source file and build an indexed help file for use
  15.  * by mm.
  16.  */
  17.  
  18. #include <stdio.h>
  19. #include "config.h"
  20. #include "osfiles.h"
  21. #include "compat.h"
  22. #include "help.h"
  23.  
  24. long str_start;                /* where the help strings start */
  25. hlp_offset *offsets;            /* indexes into file, strlens */
  26. int numtopics;                /* number of commands */
  27.  
  28. char buf[BUFSIZ];
  29.  
  30. /*
  31.  * main:
  32.  */
  33.  
  34. main (argc, argv)
  35. int argc;
  36. char **argv;
  37. {
  38.     char *infile, *outfile;
  39.     FILE *INF, *OUTF;
  40.  
  41.     switch (argc) {
  42.     case 1:
  43.     infile = DEF_SRCFILE;
  44.     outfile = DEF_HLPFILE;
  45.     break;
  46.     case 2:
  47.     infile = argv[1];
  48.     outfile = DEF_HLPFILE;
  49.     break;
  50.     case 3:
  51.     infile = argv[1];
  52.     outfile = argv[2];
  53.     break;
  54.     default:
  55.     fprintf (stderr, "usage: %s [ source [ destination ] ]\n", argv[0]);
  56.     exit (1);
  57.     }
  58.     printf ("%s -> %s\n", infile, outfile);
  59.  
  60.     if ((INF = fopen (infile, "r")) == NULL) {
  61.     perror (infile);
  62.     exit(1);
  63.     }
  64.     if ((OUTF = fopen (outfile, "w")) == NULL) {
  65.     perror (outfile);
  66.     exit(1);
  67.     }
  68.  
  69.     get_indexes(INF);
  70.     write_indexes(OUTF);
  71.     write_strings(INF,OUTF);
  72.     fclose (INF);
  73.     fclose(OUTF);
  74.     exit(0);
  75. }
  76.  
  77. /*
  78.  * get_indexes:
  79.  * snarf in the file and find out the lengths, plus what the offsets will be
  80.  * for all the helpstrings
  81.  */
  82. get_indexes (in)
  83. FILE *in;
  84. {
  85.     long cur_pos;
  86.     int cmd_xxx, len, eostring, i;
  87.     int l[3];
  88.     int n;
  89.  
  90.     do {                /* assume we get the whole line */
  91.     if (fgets (buf, BUFSIZ, in) == NULL) {
  92.         fprintf (stderr, "Premature end of input file\n");
  93.         exit(1);
  94.     }
  95.     } while (strncmp (buf, "@@@@", 4) != 0); /* start of data */
  96.  
  97.     if (sscanf (buf, "@@@@%d", &numtopics) != 1) {
  98.     fprintf (stderr,"Bad format in source file -- can't find numtopics\n");
  99.     exit(1);
  100.     }
  101.     if ((offsets = (hlp_offset *) 
  102.      malloc(numtopics*HELPLEVEL*sizeof(hlp_offset))) == NULL) {
  103.     fprintf (stderr, "No room for index array!\n");
  104.     exit(1);
  105.     }
  106.  
  107.     for (i = 0; i < numtopics*HELPLEVEL; i++)
  108.     offsets[i].offset = -1;        /* signal no string yet */
  109.  
  110.     cur_pos = 0;            /* no strings seen yet */
  111.  
  112.     do {                /* find first string start */
  113.     if (fgets (buf, BUFSIZ, in) == NULL) {
  114.         fprintf (stderr, "No strings found, aborting\n");
  115.         exit(1);
  116.     }
  117.     } while (strncmp (buf, "@@", 2) != 0);
  118.     str_start = ftell (in);        /* remember for pass 2 */
  119.  
  120.     /* now buf has the header line of a string */
  121.     while (strncmp (buf, "@@@@", 4) != 0) { /* till no more strings */
  122.     n=sscanf (buf, "@@%d %d %d %d", &cmd_xxx, &l[0], &l[1], &l[2]);
  123.     if (n < 2) {
  124.         fprintf (stderr, "Bad header format %s", buf);
  125.         exit(1);
  126.     }
  127.     for (i = 0; i < n-1; i++)
  128.         offsets[cmd_xxx*HELPLEVEL+l[i]].offset = cur_pos;
  129.  
  130.     len = 0;
  131.     eostring = FALSE;
  132.     do {
  133.         if (fgets (buf, BUFSIZ, in) == NULL) {
  134.         fprintf (stderr, "End of file in help string, aborting.\n");
  135.         exit(1);
  136.         }
  137.         if (strncmp (buf, "@@", 2) == 0) /* end of string */
  138.         eostring = TRUE;
  139.         else
  140.         len += strlen (buf);
  141.     } while (!eostring);
  142.     for (i = 0; i < n-1; i++)
  143.         offsets[cmd_xxx*HELPLEVEL+l[i]].length = len;
  144.     cur_pos += len + INTERLEN;    /* string plus interstring chars */
  145.     }
  146.     return;
  147. }
  148.  
  149. /*
  150.  * write_indexes:
  151.  * write all the indexes out in a file
  152.  */
  153. write_indexes (out)
  154. FILE *out;
  155. {
  156.     int i, n;
  157.  
  158.     fprintf (out, "%d,%d\n", numtopics, HELPLEVEL); /* for sanity checks */
  159.  
  160.     n = numtopics*HELPLEVEL;
  161.     for (i = 0; i < n; i++)
  162.     fprintf (out, "%d,%d\n", offsets[i].offset, offsets[i].length);
  163. }
  164.  
  165. /*
  166.  * write_strings:
  167.  * snarf the strings from the input to the output
  168.  */
  169. write_strings (in, out)
  170. FILE *in, *out;
  171. {
  172.     int eostring;
  173.  
  174.     fseek (in, str_start, SEEK_SET);    /* get to start of strings */
  175.     strcpy (buf, "@@");            /* what we're just past */
  176.     fprintf (out, "@@\n");
  177.  
  178.     /* ready to read the first line of a string */
  179.     while (strncmp (buf, "@@@@", 4) != 0) { /* till no more strings */
  180.     eostring = FALSE;
  181.     do {
  182.         if (fgets (buf, BUFSIZ, in) == NULL) {
  183.         fprintf (stderr, 
  184.              "End of file writing help strings, aborting.\n");
  185.         exit(1);
  186.         }
  187.         if (strncmp (buf, "@@", 2) == 0) /* end of string */
  188.         eostring = TRUE;
  189.         else            /* part of string, print it */
  190.         fwrite (buf, sizeof (char), strlen (buf), out);
  191.     } while (!eostring);
  192.     fprintf (out, "@@\n");        /* inter string marker */
  193.     }
  194.     return;
  195. }
  196.